//
//  HonorViewController.m
//  HomePage
//
//  Created by HanHaiDi on 13-8-21.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "HonorViewController.h"

@interface HonorViewController ()

@end

@implementation HonorViewController
@synthesize rootPath;

-(id)initWithFrame:(CGRect)frame root:(NSString*) path{
    self = [super init];
    if (self) {
        self.view.frame = frame;
        
        // paperbutton
        paperButton = [UIButton buttonWithType:UIButtonTypeCustom];
        paperButton.frame = CGRectMake(40, 0, 100, 50);
        paperButton.tag = 1;
        [paperButton setTitle:@"报纸类" forState:UIControlStateNormal];
        //[paperButton setBackgroundColor:[UIColor redColor]];
        
        [paperButton setTitleColor:[UIColor colorWithWhite:.62 alpha:1] forState:UIControlStateNormal];
        [paperButton setBackgroundImage:[[UIImage imageNamed:@"small_normal_tabbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                        forState:UIControlStateNormal];
        
        [paperButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [paperButton setBackgroundImage:[[UIImage imageNamed:@"small_selected_tabbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                        forState:UIControlStateHighlighted];
        
        [paperButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [paperButton setBackgroundImage:[[UIImage imageNamed:@"small_selected_tabbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                        forState:UIControlStateSelected];
//        [paperButton setTitleColor:[UIColor colorWithWhite:.62 alpha:1] forState:UIControlStateNormal];
//        [paperButton setBackgroundImage:[UIImage imageNamed:@"small_normal_tabbar"] forState:UIControlStateNormal];
//        [paperButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
//        [paperButton setBackgroundImage:[UIImage imageNamed:@"small_selected_tabbar"] forState:UIControlStateSelected];
//        [paperButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
//        [paperButton setBackgroundImage:[UIImage imageNamed:@"small_selected_tabbar"] forState:UIControlStateHighlighted];
        [self.view addSubview:paperButton];
        [paperButton addTarget:self action:@selector(onTapOption:) forControlEvents:UIControlEventTouchUpInside];
        
        // magazinebutton
        magazineButton = [UIButton buttonWithType:UIButtonTypeCustom];
        magazineButton.frame = CGRectMake(140+15, 0, 100, 50);
        magazineButton.tag = 2;
        [magazineButton setTitle:@"杂志类" forState:UIControlStateNormal];
        
        [magazineButton setTitleColor:[UIColor colorWithWhite:.62 alpha:1] forState:UIControlStateNormal];
        [magazineButton setBackgroundImage:[[UIImage imageNamed:@"small_normal_tabbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                               forState:UIControlStateNormal];
        
        [magazineButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [magazineButton setBackgroundImage:[[UIImage imageNamed:@"small_selected_tabbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                               forState:UIControlStateHighlighted];
        
        [magazineButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [magazineButton setBackgroundImage:[[UIImage imageNamed:@"small_selected_tabbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                               forState:UIControlStateSelected];

        [self.view addSubview:magazineButton];
        [magazineButton addTarget:self action:@selector(onTapOption:) forControlEvents:UIControlEventTouchUpInside];

        // networkbutton
        networkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        networkButton.frame = CGRectMake(255+15, 0, 100, 50);
        networkButton.tag = 3;
        [networkButton setTitle:@"网络类" forState:UIControlStateNormal];
        [networkButton setTitleColor:[UIColor colorWithWhite:.62 alpha:1] forState:UIControlStateNormal];
        [networkButton setBackgroundImage:[[UIImage imageNamed:@"small_normal_tabbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                               forState:UIControlStateNormal];
        
        [networkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [networkButton setBackgroundImage:[[UIImage imageNamed:@"small_selected_tabbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                               forState:UIControlStateHighlighted];
        
        [networkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [networkButton setBackgroundImage:[[UIImage imageNamed:@"small_selected_tabbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2) resizingMode:UIImageResizingModeStretch]
                               forState:UIControlStateSelected];
        [self.view addSubview:networkButton];
        [networkButton addTarget:self action:@selector(onTapOption:) forControlEvents:UIControlEventTouchUpInside];
        
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(40, 50-4, 944, 4)];
        [lineView setBackgroundColor:[UIColor redColor]];
        [self.view addSubview:lineView];
        [lineView release];
        
        self.rootPath = path;
        
        [paperButton sendActionsForControlEvents:UIControlEventTouchUpInside];


    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

-(void) onTapOption:(UIButton*)sender{
    [self refreshView];
    
    switch (sender.tag) {
        case 1:
            [paperButton setSelected:YES];
            if (paperView == nil) {
                NSString* filePath = [NSString stringWithFormat:@"%@/Paper.tpl",self.rootPath];
                paperView = [[HonorTableView alloc] initWithFrame:CGRectMake(65, 50+20, 944, self.view.frame.size.height-50) templeteFilePath:filePath];
                
            }
            [self.view addSubview:paperView];
            
            break;
        case 2:
            [magazineButton setSelected:YES];
            if (magazineView == nil) {
                NSString* filePath = [NSString stringWithFormat:@"%@/Magazine.tpl",self.rootPath];
                magazineView = [[HonorTableView alloc] initWithFrame:CGRectMake(65, 50+20, 944, self.view.frame.size.height-50) templeteFilePath:filePath];
                
            }
            [self.view addSubview:magazineView];
            break;
        case 3:
            [networkButton setSelected:YES];
            if (networkView == nil) {
                NSString* filePath = [NSString stringWithFormat:@"%@/Network.tpl",self.rootPath];
                networkView = [[HonorTableView alloc] initWithFrame:CGRectMake(65, 50+20, 944, self.view.frame.size.height-50) templeteFilePath:filePath];
                
            }
            [self.view addSubview:networkView];
            break;
        default:
            break;
    }
}

-(void) refreshView{
    [paperButton setSelected:NO];
    [magazineButton setSelected:NO];
    [networkButton setSelected:NO];
    
    if (paperView) {
        if (paperView.superview) {
            [paperView removeFromSuperview];
        }
    }
    
    if (magazineView) {
        if (magazineView.superview) {
            [magazineView removeFromSuperview];
        }
    }
    
    if (networkView) {
        if (networkView.superview) {
            [networkView removeFromSuperview];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    self.rootPath = nil;
    [super dealloc];
}

@end
